<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no">

    <title>享云商户通</title>
    <link href='../../styles/h5/reset.min.css' rel="stylesheet" type="text/css">
    <!--以上为公共css文件-->
	
    <link href='../../styles/h5/new.css' rel="stylesheet" type="text/css">
	<style>
	.zbb-not-mask {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: transparent;
    opacity: 1;
    z-index: 100;
}

.animation-body {
    width: 2rem;
    height: 1rem;
    border-radius: 4px;
    /*text-align: center;*/
    margin: 50% auto;
    color: #fff;
    font-size: .28rem;
    background-color: rgba(51, 51, 51, 0.8);
}

.animation-body .animationimg2 {
    float: left;
    width: .3rem;
    height: .3rem;
    margin: .35rem .2rem 0 .3rem;
}
.animation-body .zz-content{
    float: left;
    line-height: .3rem;
    margin-top: .35rem;
}
	
	</style>
    <script src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
    <script type="text/javascript" src='../../scripts/h5/zepto1.0.min.js'></script>
    <!--以上为公共js文件-->
</head>
<body id="wrapper">
<div class="zz-shop-header">
    <p class="zz-shop"></p>
    <!--<p class="zz-shop-list">小西天店</p>-->
    <ul>
        <li>
            <div class="upay">
                <span class="pay1-txt">消费金额</span>
                <span class="zz-shuxian"></span>
                <div class="div-ipt">
                    <div class="inner-div2" contenteditable="false">&yen;<span class="inner-ipt"></span></div>
                </div>
            </div>
        </li>
    </ul>
</div>
<a class="zz-kefu-phone" href="javascript:;">客服电话：400-112-5883</a>
<div id="box">
    <div class="left">
        <ul class="key">
            <li>
                <p>1</p>
                <p>2</p>
                <p>3</p>
            </li>
            <li>
                <p>4</p>
                <p>5</p>
                <p>6</p>
            </li>
            <li>
                <p>7</p>
                <p>8</p>
                <p>9</p>
            </li>
            <li>
                <p class="down"></p>
                <p>0</p>
                <p>.</p>
            </li>
        </ul>
    </div>
    <div class="right">
        <ol class="drw">
            <li class="delete"></li>
            <li class="pay">
                <input type="button" class="small" id="pay" value="收款">
            </li>
        </ol>
    </div>
</div>
<div class="zz-prompt" style="display: none"></div>
<div class="zbb-not-mask" style="display:none">
    <div class="animation-body" style="">
        <img class="animationimg2" src="../../image/h5/loading.gif"/>
        <span class="zz-content">支付中</span>
    </div>
</div>


</body>

<script type="text/javascript" src='../../scripts/h5/fastClick.js'></script>
<script type="text/javascript" src='../../scripts/h5/utils.js'></script>
<script charset="utf-8" type="text/javascript" src="../../scripts/h5/public.js"></script>
<script type="text/javascript">
    var $inner_ipt = $(".inner-ipt");
    var $small = $(".small");
    var $shop = $(".zz-shop");
    var $box = $("#box");
    var $prompt = $(".zz-prompt");
    var data = '';

    countRem();
    FastClick.attach(document.body);
    $(window).resize(function () {
        countRem();
        FastClick.attach(document.body);
    });
    
    
    function countRem(desW) {
        var des = desW || 750,
                winW = document.documentElement.clientWidth;
        document.documentElement.style.fontSize = winW / des * 100 + "px";
    }

    function promptShow(html) {
        $prompt.html(html);
        $small.attr("disabled", "true");
        $prompt.css({"opacity": 1, "display": "block"});
        
        $prompt.animate({
            opacity: 0
        }, 2000,"ease-in-out",function(){
        	$prompt.css("display", "none");
            $small.removeAttr("disabled");
        });
    }
    
    function errorShow(html) {
    	$prompt.html(html);
        $small.attr("disabled", "true");
        $prompt.css({"opacity": 1, "display": "block"});
    	
        clearTimeout(timer);
        var timer = setTimeout(function(){
        	$prompt.css("display", "none");
            $small.removeAttr("disabled");
        },2000);
    }
    
    //function loading() {
    //    $(".zbb-not-mask").css("display", "block");
    //}

    function bindHTML() {
        $shop.html(data["storeShortName"]);
        $shop.attr("storeId", data["storeId"]);
        $shop.attr("merId", data["merId"]);
    }

    $.ajax({
        url: utils.ajaxUrl() + "h5/gathering/both/getStoreGathering",
        type: 'get',
        success: function (res) {
            //console.log(res);
            if (res && res.retCode == '0000') {
                data = res["storeVo"];
                bindHTML()
            } else {
                promptShow(res.retMsg);
                $small.attr("disabled", "true");
            }
        }
    });

    getSignture();
    function getSignture() {
        var targetUrl1 = location.href.split('#')[0];
        $.ajax({
            url: utils.ajaxUrl() + "h5/gathering/both/getSignture",
            data: {targetUrl: targetUrl1},
            async: false,
            type: 'post',
            success: function (res) {
                //console.log(res.noncestr);
                if (res && res.retCode == '0000') {
                    wx.config({
                        debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                        appId: res.appId, // 必填，公众号的唯一标识
                        timestamp: res.timestamp, // 必填，生成签名的时间戳
                        nonceStr: res.noncestr, // 必填，生成签名的随机串
                        signature: res.signature,// 必填，签名，见附录1
                        jsApiList: ['scanQRCode'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
                    });
                } else {
                    promptShow(res.retMsg);
                    //$small.attr("disabled", "true");
                }
            }
        });
    }

    //显示键盘
    $(".upay").on("tap", function () {
        $box.animate({bottom: "0"}, 200, 'fast');
    });

    //点击数字
    $(".key").on("tap", "p", function () {
                var $this = $(this);
                //隐藏键盘
                if ($this.hasClass('down')) {
                    $box.animate({bottom: "-15rem"}, 200, 'fast');
                    return
                }
                var text = $this.text();
                var price = $inner_ipt.html(), sum = price + text;
                var numText = sum * 1;
                var temp = $inner_ipt.html();
                var arr = new Array(); //定义一数组
                arr = temp.split("."); //字符分割
                if (arr.length == 2) {
                    if (arr[1].length >= 2) {
                        promptShow("金额格式不正确,请重新输入金额!");
                        return;
                    }
                }
                if (price.indexOf(".") < 0 && text == ".") {
                    $inner_ipt.html(sum.replace(/^\./g, "0."));
                } else if (price.indexOf(".") > 0 && text == ".") {
                    $inner_ipt.html(price);
                    promptShow("金额格式不正确,请重新输入金额!");

                } else {
                    var max_amt = '10000';
                    if (parseFloat(numText) < parseFloat(max_amt)) {
                        if (numText > 0 && price != "0.00") {
                            $inner_ipt.html(sum.replace(/^\\./g, "0.").replace(/^[0]+([1-9])/, function (a, b) {
                                return b;
                            }).replace(".", "$#$").replace(/\\./g, "").replace("$#$", ".").replace(/^(\\-)*(\\d+)\\.(\\d\\d).*$/, '$1$2.$3'));
                            if (sum.length <= 7 && numText > 0) {
                                $small.addClass("bg");
                            } else {
                                $small.removeClass("bg");
                            }
                        } else if (numText > 0 && price == "0.00") {
                            $small.removeClass("bg");

                        } else {
                            $inner_ipt.html(sum.replace(/^\\./g, "0.").replace(/^[0]+/, "0").replace(".", "$#$").replace(/\\./g, "").replace("$#$", ".").replace(/^(\\-)*(\\d+)\\.(\\d\\d).*$/, '$1$2.$3'));
                            if (sum.length <= 8 && numText > 0) {
                                $small.addClass("bg");
                            } else {
                                $small.removeClass("bg");
                            }
                        }
                    } else {
                        promptShow("超过可支付的最大金额!");
                    }
                }
            }
    );

    //删除
    $(".right").on("tap", ".delete", function () {
        var num = $inner_ipt.html();
        $inner_ipt.html(num.substr(0, num.length - 1));
        var nextNum = $inner_ipt.html(), numText = nextNum * 1;
        if (nextNum.length <= 7 && numText > 0) {
            $small.addClass("bg");
        } else {
            $small.removeClass("bg");
        }
    });

    //支付
    //wx.ready(function () {
        $("#pay").on("tap", function () {
        	//$(".zbb-not-mask").css("display", "block");
            var amountInput = $inner_ipt.html();
            if (amountInput == "0" || amountInput == "0.00" || amountInput == "" || amountInput == "0.0" || amountInput == "0.") {
                $inner_ipt.html("");
                promptShow("金额格式不正确,请重新输入金额!");
                return false;
            };
            var storeId = $shop.attr("storeId");
            var merId = $shop.attr("merId");
            var storeShortName = $shop.html();
            
            $small.attr("disabled", "true");
            
            wx.scanQRCode({
                needResult: 1,
                desc: 'scanQRCode desc',
                scanType: ["qrCode", "barCode"],
                success: function (res) {
                    //扫码后获取结果参数:htpp://xxx.com/c/?6123，截取到url中的防伪码后，赋值给Input
                    //console.log(res);
                    //alert(res+":wx");
                    
                    var url = res.resultStr;
                    var aryUrl = url.split(",");
                    var aryU = url.split("?");
                    if (aryUrl.length > 1) {
                        url = aryUrl[1];
                    }

                    if (aryU.length > 1) {
                        url = aryU[1];
                    }
                    
                    var authCode = url;
                    $(".zbb-not-mask").css("display", "block");
                    //alert(authCode+":authCodeUrl");
                    $.ajax({
                        url: utils.ajaxUrl() + "h5/gathering/both/gathering",
                        data: {
                            amount: amountInput,
                            storeId: storeId,
                            merId: merId,
                            storeShortName: storeShortName,
                            authCode: authCode
                        },
                        async: true,
                        type: 'get',
                        success: function (res) {
                        	//alert(res.retMsg + "  ：retMsg");
                            if (res && res.retCode == '0000') {
                                var data = res["orderVo"];
                                var merDate = data.merDate;
                                var amount = data.amount;
                                var productName = data.productName;
                                window.location.href = "../../html/h5/gatheringSuccess.html?merDate=" + merDate + "&amount=" + amount + "&productName=" + productName;
                                //$small.removeAttr("disabled");
                                $(".zbb-not-mask").css("display", "none");
                            } else {
                            	errorShow(res.retMsg);
                            	$(".zbb-not-mask").css("display", "none");
                            }
                        }
                    });
                },
                error: function (res) {
                	errorShow(res);
                }
            });
        });
    //});
</script>
</html>